package com.xrz.sxm.aj.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.xrz.sxm.aj.adapter.FenkeAdapter;
import com.xrz.sxm.aj.base.BaseAct;
import com.xrz.sxm.aj.entity_tab.DBHelps;

import java.util.ArrayList;
import java.util.List;

public class LiaoFaActivity extends BaseAct implements OnItemClickListener{
	private String title;
	/**传过来的病名*/
	private String bingming;

	private FenkeAdapter m_Adapter;

	@ViewInject(R.id.gridview)
	private GridView m_GridView;

	private List<String> bingmings;

	private boolean isSearch;

	/**搜索*/
	@ViewInject(R.id.llyt_search)
	private RelativeLayout llyt_search;

	/** 搜索框 */
	@ViewInject(R.id.edt_bing)
	private AutoCompleteTextView edt_bing;
	/** 搜素关键词列表 */
	private List<String> m_Search_keys = new ArrayList<String>();
	/** popup的适配器 */
	private ArrayAdapter<String> m_Search_Adapter;

	private DBHelps m_DbHelps;
	@Override
	protected int setView() {
		return R.layout.layout_liaofa;
	}

	@Override
	protected void init() {
		m_DbHelps = new DBHelps(ctx);
		if(isSearch){
			edt_bing.setText(bingming);
			llyt_search.setVisibility(View.VISIBLE);
			bingmings = m_DbHelps.search(bingming);
			m_Search_keys = m_DbHelps.getKeys();
			m_Search_Adapter = new ArrayAdapter<String>(ctx, R.layout.list_item_1,
					m_Search_keys);
			edt_bing.setAdapter(m_Search_Adapter);
		}else{
			llyt_search.setVisibility(View.GONE);
			bingmings = m_DbHelps.getFenleiByTeShu(bingming);
		}
		m_Adapter = new FenkeAdapter(this, bingmings);
		m_GridView.setOnItemClickListener(this);
		m_GridView.setAdapter(m_Adapter);
	}

	public void onsearchClick(View view){
		if(edt_bing.getText().toString().trim().equals("")){
			toastLong("请输入疾病名字");
			return;
		}
		bingmings.clear();
		bingmings.addAll(m_DbHelps.search(edt_bing.getText().toString().trim()));
		m_Adapter.notifyDataSetChanged();
	}

	@Override
	protected String setTopTitle() {
		bingming = getIntent().getStringExtra("type");
		title = getIntent().getStringExtra("title");
		isSearch = title != null;
		return title == null ? bingming : title;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		startActivity(new Intent(this, Act_Detail.class).putExtra("data", bingmings.get(arg2)));
	}

}
